package com.jsz.peini.service.user;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.jsz.peini.bean.user.UserConcern;
import com.jsz.peini.mapper.user.UserConcernMapper;

@Service
public class UserConcernService {
	
	@Resource
	private UserConcernMapper userConcernMapper;
	
	public int delete(Integer id){
		int result = userConcernMapper.delete(id);
		
		return result;
	}
	
	@Transactional
	public void delete(UserConcern concern){
		if(Integer.valueOf(1).equals(concern.getFriend())){
			userConcernMapper.delFriend(concern);
		}
		userConcernMapper.delete(concern.getId());
	}

	@Transactional
    public int insert(UserConcern userConcern){
		UserConcern tmp = new UserConcern();
		tmp.setUserId(userConcern.getOtherId());
		tmp.setOtherId(userConcern.getUserId());
		UserConcern other = userConcernMapper.selectNum(tmp);
		if(other != null){
			userConcernMapper.addFriend(userConcern);
			userConcern.setFriend(1);
		}
    	int result = userConcernMapper.insertSelective(userConcern);
		return result;
    }
    
    public UserConcern selectNum(UserConcern userConcern){
    	UserConcern userConcernMap = userConcernMapper.selectNum(userConcern);
		return userConcernMap;
    }
    
    public List<LinkedHashMap<String,Object>> selectMyConcern(String userId){
    	List<LinkedHashMap<String,Object>> objectList = userConcernMapper.selectMyConcern(userId);
    	return objectList;
    }
    
    public List<LinkedHashMap<String,Object>> selectMyFans(String userId){
    	List<LinkedHashMap<String,Object>> objectList = userConcernMapper.selectMyFans(userId);
    	return objectList;
    }
    
    public List<LinkedHashMap<String,Object>> selectMyFriends(String userId){
    	List<LinkedHashMap<String,Object>> objectList = userConcernMapper.selectMyFriends(userId);
    	return objectList;
    }
    
    public int selectCountMyConcern(String userId){
    	int result = userConcernMapper.selectCountMyConcern(userId);
    	return result;
    }
    
    public int selectCountMyFans(String userId){
    	int result = userConcernMapper.selectCountMyFans(userId);
    	return result;
    }
    
    public int selectCountMyFriends(String userId){
    	int result = userConcernMapper.selectCountMyFriends(userId);
    	return result;
    }
    
	@Cacheable(value={"userInfo"},key="7200")
	public List<LinkedHashMap<String, Object>> getRank(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return userConcernMapper.selectAllUserConcern();
	}

	public LinkedHashMap<String, Object> getRankByUserId(String userId) {
		return userConcernMapper.selectUserConcernByUserId(userId).get(0);
	}

	public int getRankPosition(String userId) {
		return userConcernMapper.selectRankPosition(userId);
	}

}
